// pages/search/search.js
let myRequest = require('../../../common/script/myRequest.js');
let api = require('../../../common/script/api.js');
const app = getApp();
Page({

  /**
   * 页面的初始数据
   */
  data: {
    placeholder: '请输入商品名称',
    searchList: [],
    inpVal: '',
    recently: false,
    showRem: false
  },
  bindKeyInput: function (e) {
    let that = this;
    if (that.data.inpVal == '') {
      wx.showToast({
        title: '请输入内容',
        icon: 'error',
        duration: 2000
      })
      return false;
    } else {
      that.data.searchList.unshift(that.data.inpVal);
      wx.setStorage({
        key: 'searchList',
        data: that.data.searchList,
      })
      that.setData({
        searchList: that.data.searchList
      })
      wx.navigateTo({
        url: `/goods/pages/searchList/searchList?search=${that.data.inpVal}`
      })

    }

  },
  isShowRem: function (e) {
    let that = this;
    if (e.detail.value.length > 0) {
      that.setData({
        showRem: true,
        inpVal: e.detail.value
      })
    } else {
      that.setData({
        showRem: false
      })
    }
  },
  //清空输入框
  cancel: function (e) {
    let that = this;
    setTimeout(() => {
      that.setData({
        inpVal: '',
        showRem: false
      })
    }, 100)
  },
  // 点击最近搜索
  clickRecent: function (e) {
    let that = this;
    wx.navigateTo({
      url: `/goods/pages/searchList/searchList?search=${e.target.dataset.name}`
    })
  },
  storage: function () {
    let that = this;
    wx.getStorage({
      key: 'searchList',
      success: function (res) {
        let arr = Array.from(new Set(res.data))
        if (arr == '') {
          that.setData({
            recently: false
          })
        } else {
          that.setData({
            recently: true
          })
        }
        that.setData({
          searchList: arr,
        })
      }
    })
  },
  removeInputValue: function () {
    let that = this;
    wx.removeStorage({
      key: 'searchList'
    })
    that.setData({
      searchList: [],
      recently: false
    })

  },
  //智能图片搜索
  searchCamera() {
    wx.chooseMedia({
      count: 1,
      mediaType: ['image'],
      sourceType: ['album', 'camera'],
      success(res) {
        // tempFilePath可以作为img标签的src属性显示图片
        const tempFilePaths = res.tempFiles[0].tempFilePath
        wx.showLoading({
          title: '模型识别中...',
        })
        wx.uploadFile({
          filePath: tempFilePaths,
          name: 'imageFile',
          url: api.http + 'searchImgUpload',
          method: "post",
          header: {
            "Content-Type": "multipart/form-data",
            'Authorization': 'Bearer ' + app.globalData.token
          },
          success(porp) {
            wx.hideLoading();
            let imgUrl = JSON.parse(porp.data)
            if (imgUrl.code == 0) {
              wx.navigateTo({
                url: `/goods/pages/searchList/searchList?fileId=${imgUrl.data.filesInfo.fileId}`
              })
            } else {
              wx.showToast({
                title: imgUrl.message,
                icon: 'error',
                duration: 2000
              })
            }
          },
          error(err) {
            console.log(err)
          }
        })
      }
    })
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {

  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    const that = this;
    that.storage()
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  // onShareAppMessage: function () {

  // }
})